You are here: TI-Nspire™ Technology Program Editor eGuide > TI-Nspire™ Technology Program Editor Guide > Getting Started with the Program Editor > Using If, Lbl, and Goto to Control Program Flow

Using If, Lbl, and Goto to Control Program Flow

The If command and several If...EndIf structures let you execute a statement or block of statements conditionally, that is, based on the result of a test (such as x>5). Lbl (label) and Goto commands let you branch, or jump, from one place to another in a function or program.

The If command and several If...EndIf structures reside on the Program Editor’s Control menu.

When you insert a structure such as If...Then...EndIf, a template is inserted at the cursor location. The cursor is positioned so that you can enter a conditional test.

If Command

To execute a single command when a conditional test is true, use the general form:

If x>5

  Disp "x is greater than 5"  À

Disp x  Á

À

Executed only if x>5; otherwise, skipped.

Á

Always displays the value of x.

In this example, you must store a value to x before executing the If command.

If...Then...EndIf Structures

To execute one group of commands if a conditional test is true, use the structure:

If x>5 Then

  Disp "x is greater than 5"  À

  2¦x&x  À

EndIf

Disp x  Á

À

Executed only if x>5.

Á

Displays the value of:

  2x if x>5

  x if x{5

Note: EndIf marks the end of the Then block that is executed if the condition is true.

If...Then...Else...EndIf Structures

To execute one group of commands if a conditional test is true and a different group if the condition is false, use this structure:

If x>5 Then

  Disp "x is greater than 5"  À

  2¦x&x  À

Else

  Disp "x is less than or equal to 5"  Á

  5¦x&x  Á

EndIf

Disp x  Â

À

Executed only if x>5.

Á

Executed only if x{5.

Â

Displays value of:
  2x if x>5
  5x if x{5

If...Then...ElseIf... EndIf Structures

A more complex form of the If command lets you test for multiple conditions. Suppose you want a program to test a user-supplied argument that signifies one of four options.

To test for each option (If Choice=1, If Choice=2, and so on), use the If...Then...ElseIf...EndIf structure.

Lbl and Goto Commands

You can also control the flow by using Lbl (label) and Goto commands. These commands reside on the Program Editor’s Transfers menu.

Use the Lbl command to label (assign a name to) a particular location in the function or program.

Lbl labelName

name to assign to this location (use the same naming convention as a variable name)

You can then use the Goto command at any point in the function or program to branch to the location that corresponds to the specified label.

Goto labelName

specifies which Lbl command to branch to

Because a Goto command is unconditional (it always branches to the specified label), it is often used with an If command so that you can specify a conditional test. For example:

If x>5

  Goto GT5  À

Disp x

--------

--------  Á

Lbl GT5

Disp "The number was > 5"

À

If x>5, branches directly to label GT5.

Á

For this example, the program must include commands (such as Stop) that prevent Lbl GT5 from being executed if x{5.